﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace API.Controllers
{
    /// <summary>
    /// 账号控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        private readonly DZJDBcontext db;

        public AccountController(DZJDBcontext context)
        {
            db = context;
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<EM_Account> GetAll()
        {
            return db.EM_Account.ToList();
        }

        /// <summary>
        /// 查找单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public EM_Account? GetAccount(Guid id)
        {
            EM_Account? Account = new EM_Account();
            Account = db.EM_Account.Where(s => s.ID == id).FirstOrDefault();
            return Account;
        }

        ////[HttpGet("like/{name}")]
        ////public List<EM_Account> GetAccounts(string name)
        ////{
        ////    List<EM_Account> Account = new List<EM_Account>();
        ////    Account = db.EM_Account.Where(s => s.Model.Contains(name)).Tolist();
        ////    return Account;
        ////}

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="Account"></param>
        /// <returns></returns>
        [HttpPost]
        public bool Add(EM_Account Account)
        {
            db.EM_Account.Add(Account);
            int result = db.SaveChanges();
            return result > 0 ? true : false;
        }

        /// <summary>
        /// 改
        /// </summary>
        /// <param name="Account"></param>
        /// <returns></returns>
        [HttpPut]
        public bool Update(EM_Account Account)
        {
            db.EM_Account.Update(Account);
            int result = db.SaveChanges();
            return result > 0 ? true : false;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public bool Delete(Guid Id)
        {
            EM_Account? Account = new EM_Account();
            Account = db.EM_Account.Where(s => s.ID == Id).FirstOrDefault();
            if (Account != null)
                db.EM_Account.Remove(Account);
            int result = db.SaveChanges();
            return result > 0 ? true : false;
        }
    }
}
